package com.tzy.for10evalue.haoyong.excelexport.consumerservice;

import com.aebiz.b2b2ccorecommon.b2b.dto.product.common.ProductConstrant;
import com.aebiz.b2b2ccorecommon.b2b.dto.product.productbrand.BrandPriorityDTO;
import com.aebiz.b2b2ccorecommon.b2b.dto.product.productbrand.ProductBrandDTO;
import com.aebiz.b2b2ccorecommon.b2b.dto.supplier.supplierinfo.SupplierMainDTO;
import com.aebiz.b2b2ccorecommon.utils.ApiParamDTO;
import com.aebiz.b2bdeliproduct.constant.KafkaTopicConstant;
import com.aebiz.b2bdeliproduct.constant.LockKeyConstant;
import com.aebiz.b2bdeliproduct.constant.ProductEventLogEnum;
import com.aebiz.b2bdeliproduct.constant.ReProductPoolChangeLogEnum;
import com.aebiz.b2bdeliproduct.model.vo.ProductPoolImportLogVo;
import com.aebiz.b2bdeliproduct.model.vo.ProductPoolImportMessage;
import com.aebiz.b2bdeliproduct.model.vo.ProductSendToSapVo;
import com.aebiz.b2bdeliproduct.service.*;
import com.aebiz.b2bdeliproduct.service.goods.ReGoodsService;
import com.aebiz.b2bdeliproduct.service.product.ReDirectoryApplyProductService;
import com.aebiz.b2bdeliproduct.service.product.ReProductBrandService;
import com.aebiz.b2bdeliproduct.service.product.ReProductService;
import com.aebiz.b2bdeliproduct.service.product.ReProductSkuAuditService;
import com.aebiz.baseframework4ms.basecrud.vo.BaseModel;
import com.aebiz.baseframework4ms.basewebapi.RetDTO;
import com.aebiz.baseframework4ms.exception.AppException;
import com.aebiz.baseframework4ms.utils.DateUtils;
import com.aebiz.baseframework4ms.utils.RetUtils;
import com.aebiz.deli.common2.b2bdelisource.webapi.SourceQuotationOrderWebApi;
import com.aebiz.deli.common2.product.model.excel.CommonImportDetailDTO;
import com.aebiz.deli.common2.product.model.excel.ProductBaseInfoExcel;
import com.aebiz.deli.common2.product.model.excel.ProductExtendDeliveryInfoExcel;
import com.aebiz.deli.common2.product.model.excel.ProductExtendInfoExcel;
import com.aebiz.deli.common2.product.model.product.ProductExtendDTO;
import com.aebiz.deli.model.price.PriceUpdateParamsDTO;
import com.aebiz.deli.model.thirdInterface.ProductInfo;
import com.aebiz.deli.model.thirdInterface.SmartCodingReq;
import com.aebiz.deli.service.ProductBatchImportDetailService;
import com.aebiz.deli.service.ProductExtendRemarkService;
import com.aebiz.deli.service.ProductTargetCostService;
import com.aebiz.deli.utils.BeanCopyUtils;
import com.aebiz.plugins.b2b.common.dto.product.category.plugins.category_p2.ProductConstant;
import com.aebiz.plugins.b2b.common.dto.product.category.plugins.category_p3.params.ProductPlatCategoryAndBrandExcelDTO;
import com.aebiz.plugins.b2b.common.dto.product.goodsinfo.plugins.goodsinfo_p2.params.ImageJsonDTO;
import com.aebiz.plugins.b2b.common.dto.product.log.ExcelBatchOperateDetailLog;
import com.aebiz.plugins.b2b.common.dto.product.log.ExcelBatchOperateLog;
import com.aebiz.plugins.b2b.common.dto.product.productpool.plugins.productpool_p2.ProductPool2DTO;
import com.aebiz.plugins.b2b.common.dto.product.productpool.plugins.productpool_p2.ProductPool2QueryDTO;
import com.aebiz.plugins.b2b.common.dto.product.productpool.plugins.productpool_p2.params.ProductPoolParam;
import com.aebiz.plugins.b2b.common.dto.product.restructure.ProductPoolAudit;
import com.aebiz.plugins.b2b.common.dto.product.restructure.ReGoodsSku;
import com.aebiz.plugins.b2b.common.dto.product.restructure.ReProductBrand;
import com.aebiz.plugins.b2b.common.dto.product.restructure.ReProductSku;
import com.aebiz.plugins.b2b.common.dto.product.restructure.dto.*;
import com.aebiz.plugins.b2b.common.dto.product.restructure.excel.*;
import com.aebiz.plugins.b2b.common.dto.product.template.plugins.template_p2.params.ProductTemplateExcelDTO;
import com.aebiz.plugins.b2b.common.otherapis.product.productpool.plugins.productpoolother_p2.ProductPool2OthersApi;
import com.aebiz.plugins.b2b.common.otherapis.supplier.supplierinfo.plugins.supplierinfoother_p2.SupplierMain2OthersApi;
import com.aebiz.plugins.b2b.common.otherapis.thirdinterface.nuonuo.SmartCodingApi;
import com.aebiz.plugins.b2b.common.webapis.price.goodsprice.plugins.GoodsPriceNewWebApi;
import com.aebiz.plugins.b2b.product.business.categorymgr.platformcategorymgr.productplatcategoryandbrand.plugins.productplatcategoryandbrand_p4.service.ProductPlatCategoryAndBrand_P4Service;
import com.aebiz.plugins.b2b.product.business.categorymgr.platformcategorymgr.productplatformcategory.plugins.productplatformcategory_p3.service.ProductPlatformCategory_P3Service;
import com.aebiz.plugins.b2b.product.business.categorymgr.platformcategorymgr.productplatformcategory.plugins.productplatformcategory_p5.service.impl.ProductPlatformCategory_P5ServiceImpl;
import com.aebiz.plugins.b2b.product.business.categorymgr.platformcategorymgr.productplatformcategory.plugins.vo.BaseProductPlatformCategoryModel;
import com.aebiz.plugins.b2b.product.business.productbrandmgr.productbrand.plugins.productbrand_p8.service.ProductBrand_P8Service;
import com.aebiz.plugins.b2b.product.business.productlog.service.ExcelBatchOperateLogService;
import com.aebiz.plugins.b2b.product.business.unitmgr.productunit.plugins.productunit_p2.service.ProductUnit_P2Service;
import com.aebiz.plugins.b2b.product.otherapis.productpool.plugins.productpoolothers_p2.service.ProductPool2OthersService;
import com.aebiz.plugins.b2b.product.webapis.category.plugins.categoryweb_p3.service.ProductPlatCategoryAndBrand3WebService;
import com.aebiz.plugins.b2b.product.webapis.productbrand.plugins.productbrandweb_p2.service.ProductBrand2WebService;
import com.aebiz.plugins.b2b.product.webapis.template.plugins.templateweb_p2.service.ProductTemplate2WebService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zengtengpeng.annotation.Lock;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 产品批量新增
 *
 * @author: sunjian
 * @create: 2020-12-04 15:22
 **/
@Service
@Slf4j
public class ProductExcelImportListener {
    private static Logger LOGGER = LoggerFactory.getLogger(ProductExcelImportListener.class);
    @Autowired
    @Lazy
    private ProductExcelImportListener productExcelImportListener;

    @KafkaListener(topics = ProductConstrant.PRODUCT_BATCH_IMPORT_NEW, id ="productBatchImportNew")
    public void productBatchImportNew(String message, MessageHeaders headers) {
        log.info("=====productBatchImportNew:{}", JSON.toJSONString(message));
        CommonImportDetailDTO commonDto = JSONObject.parseObject(message, CommonImportDetailDTO.class);//存消息的json串儿 消息内部转发的导入类型 导入人id等
        ProductBatchImportNewHandler productBatchImportNewHandler = ServiceFactory.getProductBatchImportNewHandler(commonDto.getType());//获取类型去生成对应的service
        productBatchImportNewHandler.importDetail(commonDto.getMessage());
    }

    //导入进度由消息次数(对应详情表的数量)决定
    private void saveLog(Integer status, String msg, Integer lineId, String skuNo, Integer logId, int count) {
        ExcelBatchOperateDetailLog detailLog = new ExcelBatchOperateDetailLog();
        detailLog.setStatus(status);
        detailLog.setDesc(msg == null ? "" : msg);
        detailLog.setLineId(lineId == null ? 0 : lineId);
        detailLog.setSkuNo(skuNo);
        detailLog.setTime(DateUtils.getNowTimeStr());
        detailLog.setLogId(logId);
        logService.insertDetailLog(detailLog);
        logService.increaseNum(logId, count);//详情表的数量+1
    }
}
